import type { Secret } from "../../../api/k8s/types";

import { useK8SApi } from "../../../api/k8s/k8s";
import { SourceMan } from "../providers/sourceCtl";
import type { Ref } from "vue";
import type { SecretQuery } from "@/views/domain/kubeHud/api/k8s/secret";
const { secretRepo } = useK8SApi();

class SecretProvider extends SourceMan<Secret> {
  get secretList(): Ref<Secret[]> {
    return this.srcList;
  }

  // 获取节点列表
  getSecrets = async (req?: SecretQuery) => {
    const res = await secretRepo.GetSecret({
      ...(req || this.searchQuery.value),
      continue: this.continue,
      limit: this.pager.limit.value
    });
    if (res.ok) {
      this.continue = res.data.metadata.continue;
      this.srcList.value = res.data.items;
      this.pager.total.value =
        res.data.metadata.remainingItemCount ?? res.data.items.length;
    }
  };

  // 获取节点详情
  getSecretDetails = async (query: { ns?: string; name?: string }) => {
    const res = await secretRepo.GetSecretDetail(query);
    if (res.ok) {
      this.srcDetail.value = res.data;
      this.srcYamlContent.value = this.objectToYaml(res.data);
      this.yamlDialog.value = true;
    }
  };

  // 更新节点
  updateSecret = async (data?: { namespace?: string; name?: string }) => {
    const res = await secretRepo.UpdateSecret(data);
    if (res.ok) {
      this.refreshSecretList();
    }
  };

  // delete
  deleteSecret = async (query?: { namespace?: string; name?: string }) => {
    const res = await secretRepo.DeleteSecret(query);
    if (res.ok) {
      this.refreshSecretList();
    }
  };

  refreshSecretList = async () => {
    this.continue = undefined;
    await this.getSecrets({});
  };
}

export default new SecretProvider();
